import os
import shutil
import unittest

from limber import abilities
from limber import application
from limber import componentmanager
from limber import documentmanager
from limber import viewmanager

from mocks import *

dirpath = os.path.dirname(os.path.realpath(__file__))
mock_userdir = os.path.join(dirpath,'mock-userdir')
mock_sysdir = os.path.join(dirpath,'mock-systemdir')

mock_userdir_existed = os.path.exists(mock_userdir)
mock_sysdir_existed = os.path.exists(mock_sysdir)

def setup_module():
    application._original_get_dialog = application.get_dialog
    application.get_dialog = mock_get_dialog
    application._original_MainWindow = application.MainWindow
    application.MainWindow = MockMainWindowGUI

def teardown_module():
    application.get_dialog = application._original_get_dialog
    application.MainWindow = application._original_MainWindow
    del application._original_get_dialog
    del application._original_MainWindow

    #If the user and sys mock dirs existed we leave them.
    if os.path.exists(mock_userdir) and not mock_userdir_existed:
        shutil.rmtree(mock_userdir)
    if os.path.exists(mock_sysdir) and not mock_sysdir_existed:
        shutil.rmtree(mock_sysdir)


def testInstantiate():
    app = application.Application()


def testInstantiate2():
    app = application.Application(userdir=mock_userdir,
                                  systemdir=mock_sysdir)


class TestApplication(unittest.TestCase):
    def setUp(self):
        self.app = application.Application(userdir=mock_userdir,
                                           systemdir=mock_sysdir)

    def testMainwindowProperty(self):
        self.assertTrue(isinstance(self.app.mainwindow, MockMainWindowGUI))
        self.assertRaises(AttributeError,
                          setattr, self.app, 'mainwindow', None)

    def testComponentManagerProperty(self):
        self.assertTrue(isinstance(self.app.componentmanager,
                                   componentmanager.ComponentManager))
        self.assertRaises(AttributeError,
                          setattr, self.app, 'componentmanager', None)

    def testDocumentManagerProperty(self):
        self.assertTrue(isinstance(self.app.documentmanager,
                                   documentmanager.DocumentManager))
        self.assertRaises(AttributeError,
                          setattr, self.app, 'documentmanager', None)

    def testViewManagerProperty(self):
        self.assertTrue(isinstance(self.app.viewmanager,
                                   viewmanager.ViewManager))
        self.assertRaises(AttributeError,
                          setattr, self.app, 'viewmanager', None)

    def testQuerySaveFilename(self):
        doc = MockDocument()
        application.get_dialog.set_test(0)

        fname = self.app.query_save_filename(doc)
        self.assertEqual(doc.path(), None,
                         'query_save_filename should not set the documents path.')

        self.assertEqual(fname,
                         application.get_dialog('query-save-filename').filename,
                         'Got the wrong path.')
        self.assertTrue(application.get_dialog.was_called('query-save-filename'),
                        'The dialog was not called.')

    def testQueryCloseDocument(self):
        doc = MockDocument()
        application.get_dialog.set_test(1)

        should_close = self.app.query_close_document(doc)
        self.assertTrue(should_close, 'Got the wrong response.')

    def testQueryDiscardDocument(self):
        doc = MockDocument()
        application.get_dialog.set_test(2)

        should_discard = self.app.query_discard_document(doc)
        self.assertTrue(should_discard, 'Got the wrong response.')

    def testQueryDiscardDocument2(self):
        doc = MockDocument()
        application.get_dialog.set_test(3)

        should_discard = self.app.query_discard_document(doc)
        self.assertFalse(should_discard, 'Got the wrong response.')
    
    def testAvaiableAbilities(self):
        abils = self.app.avaiable_abilities()
        self.assertEqual(sorted(abils), sorted(abilities.ABILITIES))
    
    def testAvaiableDocuments(self):
        docs = self.app.avaiable_documents_names()
        #TODO: finish this!
        pass
    
    testAvaiableDocuments.__test__ = False